package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.ReportGxlqTxhfjlbTxBaDetail;
import com.ruoyi.report_lq.domain.vo.ReportGxlqTxhfjlbTxBaDetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportGxlqTxhfjlbRhdBaDetailBo;
import com.ruoyi.report_lq.domain.vo.ReportGxlqTxhfjlbRhdBaDetailVo;
import com.ruoyi.report_lq.domain.ReportGxlqTxhfjlbRhdBaDetail;
import com.ruoyi.report_lq.mapper.ReportGxlqTxhfjlbRhdBaDetailMapper;
import com.ruoyi.report_lq.service.IReportGxlqTxhfjlbRhdBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2024-07-03
 */
@RequiredArgsConstructor
@Service
public class ReportGxlqTxhfjlbRhdBaDetailServiceImpl implements IReportGxlqTxhfjlbRhdBaDetailService {

    private final ReportGxlqTxhfjlbRhdBaDetailMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public ReportGxlqTxhfjlbRhdBaDetailVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     *
     * @param infoId
     * @return 软化点差值集合
     * @method 根据info-id 查询软化点差值集合
     */
    public List<ReportGxlqTxhfjlbRhdBaDetailVo> queryByInfoId (Long infoId) {
        QueryWrapper<ReportGxlqTxhfjlbRhdBaDetail> query = new QueryWrapper<>();
        query.eq("common_id", infoId);
        return baseMapper.selectVoList(query);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ReportGxlqTxhfjlbRhdBaDetailVo> queryPageList(ReportGxlqTxhfjlbRhdBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportGxlqTxhfjlbRhdBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportGxlqTxhfjlbRhdBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ReportGxlqTxhfjlbRhdBaDetailVo> queryList(ReportGxlqTxhfjlbRhdBaDetailBo bo) {
        LambdaQueryWrapper<ReportGxlqTxhfjlbRhdBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportGxlqTxhfjlbRhdBaDetail> buildQueryWrapper(ReportGxlqTxhfjlbRhdBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportGxlqTxhfjlbRhdBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCommonId() != null, ReportGxlqTxhfjlbRhdBaDetail::getCommonId, bo.getCommonId());
        lqw.eq(StringUtils.isNotBlank(bo.getShiybh()), ReportGxlqTxhfjlbRhdBaDetail::getShiybh, bo.getShiybh());
        lqw.eq(StringUtils.isNotBlank(bo.getDingblqrhd()), ReportGxlqTxhfjlbRhdBaDetail::getDingblqrhd, bo.getDingblqrhd());
        lqw.eq(StringUtils.isNotBlank(bo.getDiblqrhd()), ReportGxlqTxhfjlbRhdBaDetail::getDiblqrhd, bo.getDiblqrhd());
        lqw.eq(StringUtils.isNotBlank(bo.getRuanhdcz()), ReportGxlqTxhfjlbRhdBaDetail::getRuanhdcz, bo.getRuanhdcz());
        lqw.eq(StringUtils.isNotBlank(bo.getPingjz()), ReportGxlqTxhfjlbRhdBaDetail::getPingjz, bo.getPingjz());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ReportGxlqTxhfjlbRhdBaDetailBo bo) {
        ReportGxlqTxhfjlbRhdBaDetail add = BeanUtil.toBean(bo, ReportGxlqTxhfjlbRhdBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ReportGxlqTxhfjlbRhdBaDetailBo bo) {
        ReportGxlqTxhfjlbRhdBaDetail update = BeanUtil.toBean(bo, ReportGxlqTxhfjlbRhdBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量新增/修改薄膜记录子表1
     */
    public Boolean insertOrUpdateByRhdDetail1Bo(List<ReportGxlqTxhfjlbRhdBaDetailVo> detailVos, Long pId) {
        detailVos.forEach(detailVo -> {
            ReportGxlqTxhfjlbRhdBaDetail detail = new ReportGxlqTxhfjlbRhdBaDetail();
            detail.setExcelId(detailVo.getExcelId());
            detail.setCommonId(pId);
            detail.setShiybh(detailVo.getShiybh());
            detail.setDingblqrhd(detailVo.getDingblqrhd());
            detail.setDiblqrhd(detailVo.getDiblqrhd());
            detail.setRuanhdcz(detailVo.getRuanhdcz());
            detail.setPingjz(detailVo.getPingjz());
            baseMapper.insertOrUpdate(detail);
        });
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportGxlqTxhfjlbRhdBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
